package com.tanhua.manager.controller;

import com.tanhua.manager.domain.Admin;
import com.tanhua.manager.interceptors.AdminHolder;
import com.tanhua.manager.service.AdminService;
import com.tanhua.manager.vo.AdminVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
public class SystemUsersController {


    @Autowired
    private AdminService adminService;

    @GetMapping(value = "/verification" ,name = "获取验证码图片")
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws Exception{
         return adminService.verification(uuid,response);
    }

    @PostMapping(value = "/login" ,name = "登录")
    public ResponseEntity login(@RequestBody Map<String,String> map) throws Exception{
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        return adminService.login(username,password,verificationCode,uuid);
    }

    @PostMapping(value = "/profile" ,name = "获取登录人信息")
    public ResponseEntity profile() throws Exception{
        Admin admin = AdminHolder.getAdmin();
        AdminVo adminVo = new AdminVo();
        BeanUtils.copyProperties(admin,adminVo);

//        又一次把token保存到redis中，续命
        adminService.saveTokenToRedis(admin,admin.getToken());

        return ResponseEntity.ok(adminVo);
    }

    @PostMapping(value = "/logout" ,name = "退出登录")
    public ResponseEntity logout() throws Exception{
        return adminService.logout();
    }

}
